var zNodes = "";
//*-----------------------------分页查询----------------------------------------------
$(document).ready(function(){
	  // 在这里写你的代码...
	
	var contextPath = Com_Parameter.ContextPath;
	var pagination = function(pageNo,pageSize,berthId,mechId){
		var floorId = $("#floorId").val();
		var storeId = $("#storeId").val();
		$.ajax({
			type: "GET",
			url:  contextPath+"/biz/contract/commonContract/showModalPage?pageSize="+pageSize+"&pageNo="+pageNo,
			data: {"berthId":berthId,"mechId":mechId,"floor":floorId,"siteid":storeId},
			success: function(data) {
				var berthObj = eval('(' + data + ')');
				var berthList = berthObj.jsonRoot;
				$("#pageTbody_berth").find("tr").each(function(){
					$(this).remove();
				});

				var tbody =$("#pageTbody_berth")
				for(var i=0;i<berthList.length;i++ ){
					var berthMaindata = berthList[i];
					var tr = "<tr>";
					tr  +="<td><input type=\"checkbox\" onclick=\"olkValue(this)\" name=\"berthCheck\" value=\""+berthMaindata.mechId+"\" datearea=\""+berthMaindata.area+"\"></td>";
					tr	+="<td>"+berthMaindata.berthId+"</td>";
					tr	+="<td>"+berthMaindata.mechId+"</td>";
//					tr	+="<td>"+berthMaindata.siteid+"</td>";
//					tr	+="<td>"+berthMaindata.floor+"</td>";
					tr	+="<td><label id=\"area_"+berthMaindata.mechId+"\">"+berthMaindata.area+"</label></td>";
					tr  +="<td style=\"display: none;\"> <label id=\"adLength"+i+"\">"+berthMaindata.adLength+"</label><label id=\"adWeight"+i+"\">"+berthMaindata.adWeight+"</label><label id=\"adStandardvalue"+i+"\">"+berthMaindata.adStandardvalue+"</label></td>";
					tr	+="</tr>" ;
					tbody.append(tr);
				}
				var total =parseFloat(berthObj.total);
				var totalSize = Math.ceil(total/parseFloat(berthObj.pageSize));
		
				$("#pageNo_berth").val(berthObj.pageNo);
				$("#pageSize_berth").val(berthObj.pageSize);
				$("#total_berth").val(berthObj.total);
				$("#pageNoSpan_berth").text("第"+berthObj.pageNo+"页");
				$("#totalSizeSpan_berth").text("共"+totalSize+"页");
			}
		});
	}
	
	
	
	//条件查询
	$("#queryBerth").click(function(){
		var berthId = $("#berthid").val();
		var mechId = $("#mechid").val();
		var pageNo = 1;
		var pageSize = parseFloat($("#pageSize_berth").val());
		pagination(pageNo,pageSize,berthId,mechId);
		ckBox();
	});
	//上一页
	$("#lastPage_berth").click(function(){
		var berthId = $("#berthid").val();
		var mechId = $("#mechid").val();
		var pageNo = parseFloat($("#pageNo_berth").val());
		var pageSize = parseFloat($("#pageSize_berth").val());
		var total =parseFloat($("#total_berth").val());
		var nextPageNo = pageNo + 1;
		var lastPageNo = pageNo -1;
		var totalSize = Math.ceil(total/pageSize);
		if(pageNo <=1){
			top.$.jBox.tip('没有上一页','warning');
			return;
		}
		pagination(lastPageNo,pageSize,berthId,mechId);
	});
	
	//跳转到指定页面
	$("#goto_berth").click(function(){
		var berthId = $("#berthid").val();
		var mechId = $("#mechid").val();
		
		var pageNo = $("#gotoPageNo_berth").val();
		var pageSize = parseFloat($("#pageSize_berth").val());
		var total =parseFloat($("#total_berth").val());
		var totalSize = Math.ceil(total/pageSize);
		if(pageNo ==null && ''==pageNo){
			top.$.jBox.tip('请输入页码','warning');
			return;
		}else if(pageNo >totalSize ||pageNo<1){
			top.$.jBox.tip('你输入的页码不在范围内','warning');
			return;
		}
		pagination(pageNo,pageSize,berthId,mechId);
	});
	
	//下一页
	$("#nextPage_berth").click(function(){
		var berthId = $("#berthid").val();
		var mechId = $("#mechid").val();
		var pageNo = parseFloat($("#pageNo_berth").val());
		var pageSize = parseFloat($("#pageSize_berth").val());
		var total =parseFloat($("#total_berth").val());
		var nextPageNo = pageNo + 1;
		var lastPageNo = pageNo -1;
		var totalSize = Math.ceil(total/pageSize);
		if(pageNo >=totalSize){
			top.$.jBox.tip('没有下一页','warning');
			return;
		}
		pagination(nextPageNo,pageSize,berthId,mechId);
	});
});

//-----------------下拉框选择商铺号------------------
function selectBerth(mechIds, contentPath){
	var obj = getBerth(mechIds, contentPath);
	zNodes = obj.zNodes;
	$.fn.zTree.init($("#treeDemo"), setting, eval("("+zNodes+")"));
}

function showMenu() {
	var cityObj = $("#shopName");
	var cityOffset = $("#shopName").offset();
	$("#menuContent").css({left:cityOffset.left-265 + "px", top:130 + cityObj.outerHeight()-1 + "px"}).slideDown("fast");
	$("body").bind("mousedown", onBodyDown);
}

var setting = {
	check: {
		enable: true,
		chkboxType: {"Y":"", "N":""}
	},
	view: {
		dblClickExpand: false
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		beforeClick: beforeClick,
		onCheck: getOptionValue
	}
};

function beforeClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.checkNode(treeNode, !treeNode.checked, null, true);
	return false;
}

function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}

//function selectBerth(floor, contentPath){
//	buildSelect(floor,contentPath);
//	$("#berth").multiselect();
//}

//function buildSelect(floor, contextPath){
//	var str = "<select id=\"berth\" name=\"berth\" multiple=\"multiple\">";
//	var list = getBerth(floor, contextPath);
//	for(var i=0; i<list.length; i++){
//		str += "<option value='"+list[i].mechId+"' title='"+list[i].berthId+"'>"+list[i].berthId+"</option>";
//	}
//	str += "</select> ";
//	$("#selectBerthDiv").html(str);
//}

/**
 * 根据招商编号查询
 */
function getBerth(mechIds, contextPath) {
	var obj=null;
	$.ajax({
		type : "GET",
		async : false,// 同步
		url : contextPath+"/sys/base/berthmaindata/queryByIds?mechIds="+mechIds,
		context : document.body,
		success : function(result) {
			obj = result;
		}
	});
	return obj;
}

function getOptionValue(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getCheckedNodes(true),
	name = "";
	value = "";
	for (var i=0, l=nodes.length; i<l; i++) {
		name += nodes[i].name + ",";
		value += nodes[i].id + ",";
	}
	if (name.length > 0 ) name = name.substring(0, name.length-1);
	if (value.length > 0 ) value = value.substring(0, value.length-1);
	$("#shopName").val(name);
	$("#assShopIds").val(value);
}

function getOptionText(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getCheckedNodes(true),
	v = "";
	for (var i=0, l=nodes.length; i<l; i++) {
		v += nodes[i].name + ",";
	}
	if (v.length > 0 ) v = v.substring(0, v.length-1);
	
	return v;
}

//获得选中铺位的value
function getCheckedValue(checkName){
	 var strValue="";
     $("input[name='"+checkName+"']:checkbox").each(function(){ 
         if($(this).attr("checked")){
        	 strValue += $(this).val()+",";
         }
     });
     if(strValue != null && strValue != ""){
    	 strValue = strValue.substring(0, strValue.length-1);
     }
     return strValue;
}

function getCheckedBerthInfo(checkName){
	 var strText="";
	var i=0;
   $("input[name='"+checkName+"']:checkbox").each(function(){ 
       if($(this).attr("checked")){
       	strText += "<tr><td></td><td>"+$("#berthId"+i).text()+"</td><td>"+$("#adLength"+i).text()+"</td><td>"+$("#adWeight"+i).text()+"</td><td>"+$("#adStandardvalue"+i).text()+"<td></tr>";
       }
       i++;
   });
   $("#contractCommnWheres").html(strText);
}


function olkValue(ts){
	if(ts.checked){
//		var value = getCheckedValue("berthCheck");
		//处理铺位
		var value = ts.value;
		var shopIds = $("#shopIds").val()+",";
		value = shopIds+value;
		
		//计算合同签订面积
		var area = $(ts).attr("datearea");
		var areaVal = $("#contractArea").val();
		$("#contractArea").val(Number(area)+Number(areaVal));
	}else{
		//处理铺位
		var value = ts.value;
		var shopIds = $("#shopIds").val();
		value = shopIds.replace(value, "");
		
		//计算合同签订面积
		var area = $(ts).attr("datearea");
		var areaVal = $("#contractArea").val();
		$("#contractArea").val(Number(areaVal)-Number(area));
	}
	value = value.replace(",,",",");
	if(value == ","){ value = "" };
	$("#shopIds").val(value);
	$("#shop").val(value);
}

function setShopValue(text){
	var text = getCheckedText("berthCheck");
	
	var str = text.split(",");		//计算总面积
	var area = 0;
	for(var i=0;i<str.length; i++){
		area += Number(str[i]);
	}
	$("#contractArea").val(area);
	getCheckedBerthInfo("berthCheck");
} 

//获得选中铺位的text 铺位号
function getCheckedText(checkName){
	 var strText="";
    $("input[name='"+checkName+"']:checkbox").each(function(){ 
        if($(this).attr("checked")){
        	strText += $(this).attr("datearea")+",";
        }
    });
    if(strText != null && strText != ""){
    	strText = strText.substring(0, strText.length-1);
    }
    return strText;
}

//---------------------品牌信息------------------------
function setBrandValue(contextPath){
	var id = $("#brand").find("option:selected").val();
	var shopId =$("#storeId").val()
	
	var obj = getBrandInfo(id,shopId,contextPath);
	if(obj != null){
		$("#inviteCate").val(obj.cmcCateId);
		$("#brandId").val(obj.brandId);
		$("#brandNo").val(obj.brandNo);
		$("#brandCName").val(obj.brandCName);
//		$("#name").val(obj.brandCName);
		$("#brandEName").val(obj.brandEName);
		$("#brandLevel").val(obj.levelId);
		$("#inviteCateName").val(obj.cmcCateName);
//		$("#name").val(obj.cmcCateName);
		$("#gcardDiscount").val(obj.gcardDiscount);
		$("#ocardDiscount").val(obj.ocardDiscount);
		$("#brandInviteHead").val(obj.brandInviteHead);
		
	}
	
	$("#memberRateType").children("option").each(function(){
		if($(this).val() == "10"){
			$(this).attr("selected","selected");
		}
	});
	
}

function getBrandInfo(id,shopId,contextPath) {
	var obj=null;
	$.ajax({
		type : "GET",
		async : false,// 同步
		url : contextPath+"/biz/contract/commonContract/queryBrandInfo?id="+id+"&shopId="+shopId,
		context : Object,
		success : function(result) {
			obj = result;
		}
	});
	return obj;
}

function setCMCate(){
	var text = $("#brand").find("option:selected").text();
	text = text.split("/");
	var cateName = "";
	if(text.length > 0 ){
		cateName = text[1];
	}
	$("#inviteCateName").val(cateName);
}

//function showBerthInfo(contextPath){
//	var shopName = $("#shop").val();
//	var strText = "";
//	var obj = getBerth(shopName, contextPath);
//	var objList = obj.list;
//	if(objList != null){
//		for(var i=0; i<objList.length; i++){
//			strText += "<tr><td></td><td>"+objList[i].berthId==null?'':objList[i].berthId+"</td><td>"+objList[i].adLength+"</td><td>"+objList[i].adWeight+"</td><td>"+objList[i].adStandardvalue+"<td></tr>";
//		}
//		$("#contractCommnWheres").html(strText);
//	}
//}

function showBerthInfo() {
	var contextPath = Com_Parameter.ContextPath;
	var shopName = $("#shop").val();
	var strText = "";
	var obj = getBerth(shopName, contextPath);
	var objList = obj.list;
	if (objList != null) {
		for (var i = 0; i < objList.length; i++) {
			if(objList[i] != "" && objList[i] != null){
			var berthId=objList[i].berthId==null?'':objList[i].berthId
				strText += "<tr><td></td><td>"+berthId+"</td><td>" + objList[i].adLength + "</td><td>"
						+ objList[i].adWeight + "</td><td>"
						+ objList[i].adStandardvalue + "<td></tr>";
			}
		}
		$("#contractCommnWheres").html(strText);
	}
}


function isMainBrand(){				//处理主品牌
	for(var i=0; i<num; i++){
		var isMainBrand = $("#mainBarnd"+i).val();
		var brand = $("#brand").val(); 
		var modal_mainBarnd = $("#mainBarnd").val(); 
		if(isMainBrand == "1"){
			$("#mainBarnd").children("option").each(function(){
				if(brand == ""){		//新签操作时。 如果已选择主品牌，则将主品牌字段默认设置为否，并且状态为不可编辑；
					if($(this).val() == "0" ){
						$(this).attr("selected","selected");
						$("#mainBarnd").attr("disabled", "disabled");
					}
				}else if(brand != "" && modal_mainBarnd != "1"){	//修改操作时。 如果主品牌为“是”时，主品牌字段状态为可编辑； 如果主品牌为“否”时，主品牌字段状态为不可编辑；
					$("#mainBarnd").attr("disabled", "disabled");
				}
			});
		}
	}
}

function cleanShop(){
	$("#shop").val("");
	$("#shopIds").val("");
}
